<?php

/**
 * 国际快递公司 
 */
class Application_Model_Express extends Application_Model_DomainObject{
    
    protected $_id;
    
    protected $_name;//名称
    
    protected $_firstWeight;//首重
    
    protected $_subsequentWeight;//续重

    protected $_fuelSurcharge;//燃油费率
    
    protected $_isVolume;//是否要体积重量
    
    protected $_typeMark;//货物类型掩码 如接收些类型，进行“与”运算后为真
    
    protected $_description;//说明
    
    protected $_remark;//内部备注
    
    protected $_isDisable;//是否禁用

    protected $_logo;//图标

    protected $_reportFee;//报关费
    
    protected $_unitWeight;//每个单位重量是多少，用于在计进行单价报价方式时进行记录
    
    protected $_isSpecific;//是否特殊通道

    protected $_orderBy;
    
    protected $_isInvoice;
    
    protected $_maxWeight;
    
    protected $_webUrl;
    
    protected $_trackUrl;
    
    protected $_trackType;
    
    protected $_helpUrl;

    protected $_trackNo;
    
    protected $_isRemoteFee;

    protected $_remoteFee;  // 偏远费用

    protected $_othersFee;  // 其他费用

    protected $_es_time;    // 运输时间
    protected $_code;
    /**
     * AIR渠道Id
     */
    const Express_AIR = 4;
    /**
     * 会员卡体验渠道Id
     */
    const Expredd_ReceiveUser = 14;
    
    const Express_EastA = 30;
            
    const Express_EastB = 31;
    
    const Express_WesternA = 24;
            
    const Express_WesternB = 29;
    
    const Express_DHLBrand=6;
    
    const Express_Eub = 9;
    
    const Express_AEub = 13;

    /**
     * 追踪方式:快递100
     */
    const TRACK_TYPE_KUAIDI100 = 1;

    /**
     * 追踪方式:悠奇手工搞定
     */
    const TRACK_TYPE_UUCH = 2;
    
    /**
     * 追踪方式:第三方网站
     */
    const TRACK_TYPE_3TH = 3;
    
    /**
     * 追踪方式:官网
     */
    const TRACK_TYPE_WEB = 4;
    
    /**
     * 追踪方式:混合,手工加官网
     */
    const TRACK_TYPE_MIX = 5;
    
    /**
     * 追踪链接
     */
    const TRACK_TYPE_LINK = 6;
    /**
     * 追踪方式:无追踪
     */
    const TRACK_TYPE_NONE = 7;

    /**
     * 快递100链接追踪
     */
    const TRACK_TYPE_KUAIDI_LINK = 8;
    
    /**
     * 获取 快递100 返回的JSON数据 跟踪
     */
    const TRACK_TYPE_KUAIDI100JSON = 9;     
    
    /**
     * 判断该物流是否接收此类型的货物
     * @param int $type 货物类型
     * @param Application_Model_ExpressPrice $expressPrice
     * @return bool 
     */
    public function isAccept($type,$expressPrice =NULL){
        
        if(!$type)
            $type=0;
        
        
        if($expressPrice == NULL || $expressPrice->TypeMark == NULL || $expressPrice->TypeMark == '' || $expressPrice->TypeMark == 0){
            $result =((intval($type) &intval($this->_typeMark)) == $type);
            //echo 't:'.$type.'||m:'.$this->_typeMark. '====='. (intval($type) & intval($this->_typeMark)).'======';
            return $result;
        }
        
        $result =((intval($type) &intval($expressPrice->TypeMark)) == $type);
        return $result;
    }
    
    /**
     * 根据城市ID返回对应的报价
     * @param int $cityId
     * @return array 
     */
    public static function getPricesByCity($cityId){
        return Application_Model_ExpressPrice::getPricesByCity($cityId);
    }
    
    public static function getPricesByExpress($expressId){
        return Application_Model_ExpressPrice::getPricesByExpress($expressId);
    }

    public static function getAll(){
        return self::getDao()->queryAll(array());
    }
    
    protected $_relationChannel;
    /**
     * 返回快递方式可选择的相应的渠道
     * @return type
     */
    public function getRelationChannel(){
        if($this->_relationChannel == NULL){
            $this->_relationChannel = Application_Model_ExpressChannel::getByExpressId($this->_id);
        }
        return $this->_relationChannel;
    }
    
    /**
     * 返回快递对应的各渠道报价
     * @param type $cityId
     * @param type $weight
     * @return type
     */
    public function getReportCost($cityId,$weight){
        $ids = Uuch_Util::select($this->getRelationChannel(), function($item){return $item->Id;});
        return Application_Model_ExpressCost::getCostByChannelCityWeight($ids,$cityId,$this->getWeightRange($weight));
    }
    
    /**
     * 根据给点的重量,返回他所在的重量区间
     * @param type $weight
     */
    public function getWeightRange($weight){
        //return $this->_firstWeight + ($weight - $weight % $this->_subsequentWeight);
        return 500 + ($weight - $weight % 500);
    }
    
    public static function getExpressFileds($name,$isVolume,$isDisable,$isInvoice,$isRemoteFee,$pagesize,$pageindex)
    {
        $where = array();
        $and = "";
        if($name != "") {
            $where[" name like "] = "%".$name."%";
            $and = " and ";
        }
        if($isVolume !== "") {
            $where[$and." isVolume = "] = $isVolume;
            $and = " and ";
        }
        if($isDisable !== ""){
            $where[$and." isDisable = "] = $isDisable;
            $and = " and ";
        }
        if($isInvoice !== "") {
            $where[$and." isInvoice = "] = $isInvoice;
            $and = " and ";
        }
        if($isRemoteFee !== ""){
            $where[$and." isRemoteFee = "] = $isRemoteFee;
            $and = " and ";
        }
//        var_dump($where);
        return self::getDao()->queryPage($where, " id asc", $pagesize, $pageindex);
        
    }
    
    public function getVolumeName()
    {
        if($this->_isVolume == 1){
            return "需要";
        } else {
            return "不需要";
        }
    }
    
    public function getDisableName()
    {
        if($this->_isDisable == 1){
            return "禁用";
        } else {
            return "启用";
        }
    }
    
    public function getInvoiceName()
    {
        if($this->_isInvoice == 1){
            return "需要";
        } else {
            return "不需要";
        }
    }
    
    public function getRemoteFeeName()
    {
        if($this->_isRemoteFee == 1){
            return "需要";
        } else {
            return "不需要";
        }
    }
    
    public function getTrackTypeName()
    {
        switch ($this->_trackType) {
            case 1:
                return "快递100";
                break;
            case 2:
                return "悠奇手工";
                break;
            case 3:
                return "第三方网站";
                break;
            case 4:
                return "官网";
                break;
            case 5:
                return "混合";
                break;
            case 6:
                return "无";
                break;
            default:
                return "无";
                break;
        }
    }


    public function getItemTypeName() {
        $names = Application_Model_StoreItem::getNames();
        if ($this->_typeMark == 0) {
            return $names['0'][0];
        }
        $nameAll = '';
        if ($this->_typeMark) {
            foreach ($names as $key => $name) {
                if (($this->_typeMark & $key) == $key) {
                    if($name[0] != '正常'){
                        $nameAll = $nameAll . $name[0] . '/';
                    }
                }
            }
        }
        return $nameAll;
    }
    
    /**
     * 获取信息
    */
    public static function getByCondition($condition = array())
    {
        if(!is_array($condition)){
            return false;
        }
        return self::getDao()->queryAll($condition);
    }

    /**
     * 获取数据
     * @return list
    */
    public static function getAllbyCondition($condition = array())
    {
        if(!is_array($condition)){
            return false;
        }
        return self::getDao()->queryAll($condition , ' id asc ');
    }

    /**
     * 获取设置vip价格的快递
    */
    public static function getVip() {
        $express = new Application_Model_DbTable_Express();
        return $express->getVip();
    }
}